<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax Get请求</title>

    <style>
        /* 设置div标签的样式 */
        
        #result {
            width: 200px;
            height: 200px;
            border: solid 4px olivedrab;
        }
    </style>

</head>

<body>
    <button>发送get请求</button>
    <div id="result"></div>

    <script>
        // 根据TagName获取dom对象
        const btn = document.getElementsByTagName('button')[0];
        // 根据id获取dom对象
        const div = document.getElementById('result');

        // 绑定btn的点击事件
        btn.onclick = function() {
            // 获取XMLHttpRequest对象
            const xhr = new XMLHttpRequest();
            // 设置请求方式和请求链接
            xhr.open('get', 'http://127.0.0.1:9999/server?a=1&b=2&c=3');
            // 发送请求
            xhr.send();
            // 监听页码加载完毕
            xhr.onreadystatechange = function() {
                // 监听页面加载完毕
                if (xhr.readyState === 4) {
                    // 判断响应码成功
                    if (xhr.status >= 200 && xhr.status < 300) {
                        console.log(`页面加载状态：${xhr.readyState}`);
                        console.log(`请求响应码：${xhr.status}`);
                        console.log(`请求状态值：${xhr.statusText}`);
                        console.log(`请求头：${xhr.getAllResponseHeaders()}`);
                        console.log(`请求体：${xhr.response}`);

                        // 动态加载div内容
                        div.innerHTML = xhr.response;
                    }
                }
            }
        }
    </script>
</body>

</html>